<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryRef"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="发型名称" prop="hairName">
        <el-input
          v-model="queryParams.hairName"
          placeholder="请输入发型名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="作用范围" prop="actionScope">
        <el-select
          @change="handleQuery"
          v-model="queryParams.actionScope"
          placeholder="选择作用范围"
          clearable
          style="width: 240px"
        >
          <el-option
            v-for="dict in sys_hair_action_scope"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="显示状态" prop="status">
        <el-select
          @change="handleQuery"
          v-model="queryParams.status"
          placeholder="选择显示状态"
          clearable
          style="width: 240px"
        >
          <el-option
            v-for="dict in sys_show_hide"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="发型分类" prop="hairClassify">
        <el-select v-model="queryParams.hairClassify" clearable placeholder="请选择分类">
          <el-option
            v-for="dict in classification"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          > </el-option>
        </el-select>
      </el-form-item>
      <!--      <el-form-item label="发型素材照片" prop="hairStockPhoto">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.hairStockPhoto"-->
      <!--          placeholder="请输入发型素材照片"-->
      <!--          clearable-->
      <!--          @keyup.enter="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="字典排序" prop="sort">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.sort"-->
      <!--          placeholder="请输入字典排序"-->
      <!--          clearable-->
      <!--          @keyup.enter="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['business:aiHairstyleType:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['business:aiHairstyleType:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['business:aiHairstyleType:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['business:aiHairstyleType:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        v-model:showSearch="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="aiHairstyleTypeList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <!--      <el-table-column label="序号" align="center" prop="id" v-if="true"/>-->
      <el-table-column label="作用范围" align="center" prop="actionScope">
        <template #default="scope">
          <dict-tag :options="sys_hair_action_scope" :value="scope.row.actionScope" />
        </template>
      </el-table-column>
      <el-table-column label="发型名称" align="center" prop="hairName" />
      <el-table-column label="发型类型" align="center" prop="hairType" />
      <el-table-column label="发型分类" align="center" prop="hairClassify" />
      <el-table-column label="发型描述" align="center" prop="hairDescribe" />
      <el-table-column label="发型介绍" align="center" prop="hairIntroduce" />
      <el-table-column label="是否热门" align="center" prop="whetherHot" />
      <el-table-column
        label="发型素材照片"
        align="center"
        prop="hairStockPhoto"
      >
        <template #default="scope">
          <template v-if="scope.row.hairStockPhoto != null">
            <image-preview
              :src="scope.row.hairStockPhoto"
              style="max-width: 100px; max-height: 100px"
            />
          </template>
        </template>
      </el-table-column>
      <el-table-column label="发型素材视频" align="center" prop="hairStockVideo" width="300">
        <template #default="scope">
          <template v-if="scope.row.hairStockVideo != null && scope.row.hairStockVideo != ''">
            <video
              controls
              muted
              :width="200" :height="200"
              type="video/mp4" :src="scope.row.hairStockVideo">
              Your browser doesn't support the video tag.
            </video>
          </template>

        </template>
      </el-table-column>
      <el-table-column label="适合脸型" align="center" prop="suitableFaceShape">
        <template #default="scope">
          <dict-tag :options="sys_face_shape" :value="scope.row.suitableFaceShape" />
        </template>
      </el-table-column>
      <el-table-column label="显示状态" align="center" prop="status">
        <template #default="scope">
          <dict-tag :options="sys_show_hide" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column label="显示顺序" align="center" prop="sort" />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template #default="scope">
          <el-button
            link
            type="primary"
            icon="Edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['business:aiHairstyleType:edit']"
            >修改</el-button
          >
          <el-button
            link
            type="primary"
            icon="Delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['business:aiHairstyleType:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改极客侠小程序ai发型库对话框 -->
    <el-dialog :title="title" v-model="open" width="600px" append-to-body>
      <el-form
        ref="aiHairstyleTypeRef"
        :model="form"
        :rules="rules"
        label-width="80px"
      >
        <el-form-item label="作用范围" prop="actionScope">
          <el-select v-model="form.actionScope" placeholder="请选择作用范围">
            <el-option
              v-for="dict in sys_hair_action_scope"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="发型名称" prop="hairName">
          <el-input v-model="form.hairName" placeholder="请输入发型名称" />
        </el-form-item>
        <el-form-item label="发型类型" prop="hairType">
          <el-input v-model="form.hairType" placeholder="请输入发型类型" />
        </el-form-item>
        <el-form-item label="发型介绍" prop="hairIntroduce">
          <el-input v-model="form.hairIntroduce" clearable placeholder="请输入发型介绍" />
        </el-form-item>
        <el-form-item label="发型描述" prop="hairDescribe">
          <el-input v-model="form.hairDescribe" clearable placeholder="请输入发型描述" />
        </el-form-item>
        <el-form-item label="适合脸型" prop="suitableFaceShape">
          <el-select v-model="form.suitableFaceShape" multiple placeholder="请选择脸型">
            <el-option
              v-for="dict in sys_face_shape"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="发型分类" prop="hairClassify">
          <el-select v-model="form.hairClassify" clearable placeholder="请选择分类">
            <el-option
              v-for="dict in classification"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            > </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="是否热门" prop="hairClassify">
          <el-select v-model="form.whetherHot" clearable placeholder="请选择分类">
            <el-option
              v-for="dict in ['是','否']"
              :key="dict"
              :label="dict"
              :value="dict"
            > </el-option>
          </el-select>
        </el-form-item>
        
        <el-form-item label="发型素材照片" prop="hairStockPhoto">
          <image-uploads
            @filename="filename"
            v-model="form.hairStockPhoto"
            :limit="1"
          />
        </el-form-item>
        <el-form-item label="发型素材视频" prop="hairStockVideo">
          <videoUpload v-model="form.hairStockVideo"></videoUpload>
        </el-form-item>
        <el-form-item label="显示顺序" prop="sort">
          <el-input-number
            v-model="form.sort"
            controls-position="right"
            :min="0"
          />
        </el-form-item>
        <el-form-item label="显示状态">
          <el-radio-group v-model="form.status">
            <el-radio
              v-for="dict in sys_show_hide"
              :key="dict.value"
              :label="dict.value"
              >{{ dict.label }}</el-radio
            >
          </el-radio-group>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button :loading="buttonLoading" type="primary" @click="submitForm"
            >确 定</el-button
          >
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="AiHairstyleType">
import {
  listAiHairstyleType,
  getAiHairstyleType,
  delAiHairstyleType,
  addAiHairstyleType,
  updateAiHairstyleType,
} from "@/api/business/aiHairstyleType";
import ImageUploads from "@/components/ImageUploads/index.vue";
import VideoUpload from "@/components/VideoUpload/index.vue";

const { proxy } = getCurrentInstance();
const { sys_show_hide,sys_face_shape,sys_hair_action_scope,classification } = proxy.useDict("sys_show_hide","sys_face_shape","sys_hair_action_scope","classification");
const aiHairstyleTypeList = ref([]);
const open = ref(false);
const buttonLoading = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    hairName: undefined,
    hairStockPhoto: undefined,
    status: undefined,
    sort: undefined,
    hairType: undefined,
  },
  rules: {
    id: [{ required: true, message: "不能为空", trigger: "blur" }],
    hairName: [
      { required: true, message: "发型名称不能为空", trigger: "blur" },
    ],
    hairStockPhoto: [
      { required: true, message: "发型素材照片不能为空", trigger: "blur" },
    ],
    status: [
      {
        required: true,
        message: "状态(0:显示 1:隐藏)不能为空",
        trigger: "change",
      },
    ],
    sort: [{ required: true, message: "字典排序不能为空", trigger: "blur" }],
    hairType: [
      { required: true, message: "发型类型不能为空", trigger: "change" },
    ],
    hairIntroduce: [
      { required: true, message: "发型介绍不能为空", trigger: "blur" },
    ],
  },
});

const { queryParams, form, rules } = toRefs(data);

function filename(val) {
  // console.log(2222,val)
  // form.value.hairType = val.split(".")[0]
  // form.value.hairName = val.split(".")[0]
  // console.log(form.value.hairType,2222,val)
}

/** 查询极客侠小程序ai发型库列表 */
function getList() {
  loading.value = true;
  listAiHairstyleType(queryParams.value).then((response) => {
    aiHairstyleTypeList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    hairName: null,
    hairStockPhoto: null,
    status: "0",
    actionScope: "0",
    sort: 1,
    hairType: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null,
  };
  proxy.resetForm("aiHairstyleTypeRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map((item) => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加极客侠小程序ai发型库";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  loading.value = true;
  reset();
  const _id = row.id || ids.value;
  getAiHairstyleType(_id).then((response) => {
    loading.value = false;
    form.value = response.data;
    form.value.status = form.value.status + "";
    if(form.value.suitableFaceShape){
      form.value.suitableFaceShape = form.value.suitableFaceShape.split(",")
    }
    open.value = true;
    title.value = "修改极客侠小程序ai发型库";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["aiHairstyleTypeRef"].validate((valid) => {
    if (valid) {
      buttonLoading.value = true;
      if(form.value.suitableFaceShape){
        form.value.suitableFaceShape = form.value.suitableFaceShape.join(",")
      }
      if(form.value.hairClassify=='热门分类'||form.value.hairClassify=='精致儒雅'||form.value.hairClassify=='时尚油头'){
        form.value.whetherHot = '是'
      }
      if (form.value.id != null) {
        updateAiHairstyleType(form.value)
          .then((response) => {
            proxy.$modal.msgSuccess("修改成功");
            open.value = false;
            getList();
          })
          .finally(() => {
            buttonLoading.value = false;
          });
      } else {
        addAiHairstyleType(form.value)
          .then((response) => {
            proxy.$modal.msgSuccess("新增成功");
            open.value = false;
            getList();
          })
          .finally(() => {
            buttonLoading.value = false;
          });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal
    .confirm('是否确认删除极客侠小程序ai发型库编号为"' + _ids + '"的数据项？')
    .then(function () {
      loading.value = true;
      return delAiHairstyleType(_ids);
    })
    .then(() => {
      loading.value = true;
      getList();
      proxy.$modal.msgSuccess("删除成功");
    })
    .catch(() => {})
    .finally(() => {
      loading.value = false;
    });
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download(
    "business/aiHairstyleType/export",
    {
      ...queryParams.value,
    },
    `aiHairstyleType_${new Date().getTime()}.xlsx`
  );
}

getList();
</script>
